package com.wenyu.wenyuyuedu.activity.personage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.LoginActivity;
import com.wenyu.wenyuyuedu.bean.personage.PersonageBeginBean;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.personage.PersonageBeginServer;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;
import com.wenyu.wenyuyuedu.utils.StatusBarUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static anetwork.channel.http.NetworkSdkSetting.context;
import static com.wenyu.wenyuyuedu.R.id.rl_meassage_zan;

/*消息通知*/
public class AlertsActivity extends AppCompatActivity implements View.OnClickListener {

    @InjectView(R.id.member_left)
    ImageView memberLeft;
    @InjectView(R.id.tv_message_commetncount)
    TextView tvMessageCommetncount;
    @InjectView(R.id.rl_meassage_comment)
    RelativeLayout rlMeassageComment;
    @InjectView(R.id.tv_message_replycount)
    TextView tvMessageReplycount;
    @InjectView(R.id.rl_meassage_reply)
    RelativeLayout rlMeassageReply;
    @InjectView(R.id.tv_message_zan)
    TextView tvMessageZan;
    @InjectView(rl_meassage_zan)
    RelativeLayout rlMeassageZan;
    @InjectView(R.id.tv_message_playtour)
    TextView tvMessagePlaytour;
    @InjectView(R.id.rl_meassage_palytour)
    RelativeLayout rlMeassagePalytour;
    @InjectView(R.id.tv_message_wenyu)
    TextView tvMessageWenyu;
    @InjectView(R.id.rl_meassage_wenyu)
    RelativeLayout rlMeassageWenyu;
    private Intent intent;
    public Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alerts);
        StatusBarUtil.setStatusBar(AlertsActivity.this, R.color.white);
        ButterKnife.inject(this);
        activity = this;
        initContrl();
    }

    /*初始化控件的点击事件*/
    private void initContrl() {
        intent = new Intent();
        memberLeft.setOnClickListener(this);
        rlMeassageComment.setOnClickListener(this);
        rlMeassageReply.setOnClickListener(this);
        rlMeassageZan.setOnClickListener(this);
        rlMeassagePalytour.setOnClickListener(this);
        rlMeassageWenyu.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        requestData();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //requestData();
    }

    /*展示每条信息的具体条数的数据*/
    private void requestData() {
        String token = AbSharedUtil.getString(AlertsActivity.this, SharedConstants.USER_TOKEN);
        PersonageBeginServer personageBeginServer = ApiWrapper.getInstance().create(PersonageBeginServer.class);
        Call<PersonageBeginBean> begin = personageBeginServer.getBegin(token, 3);
        begin.enqueue(new Callback<PersonageBeginBean>() {
            @Override
            public void onResponse(Call<PersonageBeginBean> call, Response<PersonageBeginBean> response) {
                PersonageBeginBean body = response.body();
                if (body != null) {
                    int code = body.getCode();
                    if (code == 1) {
                        int comment = body.getInfo().getInform().getComment();
                        if (comment != 0) {
                            tvMessageCommetncount.setText(body.getInfo().getInform().getComment() + "");
                            tvMessageCommetncount.setVisibility(View.VISIBLE);
                        }
                        int reply = body.getInfo().getInform().getReply();
                        if (reply != 0) {
                            tvMessageReplycount.setText(String.valueOf(reply));
                            tvMessageReplycount.setVisibility(View.VISIBLE);
                        }
                        int support = body.getInfo().getInform().getSupport();
                        if (support != 0) {
                            tvMessageZan.setText(support + "");
                            tvMessageZan.setVisibility(View.VISIBLE);
                        }
                        int reward = body.getInfo().getInform().getReward();
                        if (reward != 0) {
                            tvMessagePlaytour.setText(reward + "");
                            tvMessagePlaytour.setVisibility(View.VISIBLE);
                        }
                        int system = body.getInfo().getInform().getSystem();
                        if (system != 0) {
                            tvMessageWenyu.setText(system + "");
                            tvMessageWenyu.setVisibility(View.VISIBLE);
                        }

                    } else if (code == 0) {
                        showLoginDoalog();
                    }
                }
            }

            @Override
            public void onFailure(Call<PersonageBeginBean> call, Throwable t) {
                MyToast.showToast(activity, "网络异常，请稍后重试！", Toast.LENGTH_SHORT);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            /*左返回*/
            case R.id.member_left:
                finish();
                break;
            /*评论*/
            case R.id.rl_meassage_comment:
                intent.setClass(activity, MessageCommentActivity.class);
                startActivity(intent);
                break;
            /*回复*/
            case R.id.rl_meassage_reply:
                intent.setClass(activity, MessageReplyActivity.class);
                startActivity(intent);
                break;
            /*赞*/
            case R.id.rl_meassage_zan:
                intent.setClass(activity, MessageZanActivity.class);
                startActivity(intent);
                break;
            /*打赏*/
            case R.id.rl_meassage_palytour:
                intent.setClass(activity, MessagePlaytourActivity.class);
                startActivity(intent);
                break;
            /*系统通知*/
            case R.id.rl_meassage_wenyu:
                intent.setClass(activity, MessageWenyuActivity.class);
                startActivity(intent);
                break;
        }
    }

    android.app.AlertDialog ad;
    private TextView quxiao;
    private TextView queding;

    /*显示登陆的dialog*/
    private void showLoginDoalog() {

        ad = new android.app.AlertDialog.Builder(context).create();
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.dailog_login);
        quxiao = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        queding = (TextView) window.findViewById(R.id.dialog_login_queding);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(AlertsActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                ad.dismiss();
            }
        });
    }
}
